package com.wwwarehouse.contract.adjust_contract;


import android.os.Bundle;

import com.alibaba.fastjson.JSON;
import com.wwwarehouse.common.fragment.CommonBasePagerFragment;
import com.wwwarehouse.common.tools.ToastUtils;
import com.wwwarehouse.contract.bean.ConstractOrderDetailsBean;
import com.wwwarehouse.contract.bean.ProductDetailsBean;
import com.wwwarehouse.contract.core.ContractConstant;

import java.util.HashMap;
import java.util.Map;

import contract.wwwarehouse.com.contract.R;

/**
 * Creator by zhenwang.xiang on 2017/10/18 15:39
 * Description: 商品明细viewpager
 */


public class ConstractProductDetailsViewPagerFragment extends CommonBasePagerFragment {

    private String contractUkid;
    private ConstractOrderDetailsBean mOrderDetailsBean;

    @Override
    public void requestDatas() {
        super.requestDatas();
        if (getArguments() != null) {
            mOrderDetailsBean = getArguments().getParcelable("orderbean");
            contractUkid = getArguments().getString("contractUkid");
        }

        //网络请求
        requestHttp();

    }

    @Override
    public void setTitle() {
        super.setTitle();
        if (peekFragment() instanceof ConstractProductDetailsViewPagerFragment) {
            mActivity.setTitle(getString(R.string.constract_product_details_title));
        }
    }


    private void requestHttp() {
        Map<String, Object> map = new HashMap<>();
        map.put("contractUkid", contractUkid);
        map.put("page", "1");
        map.put("size", "4");
        loadData(ContractConstant.GET_ITEMDETAILS, map, 0);
    }


    @Override
    public void onRefreshListener() {
        requestHttp();
    }

    @Override
    public void onDownPull() {

    }

    @Override
    public void onLoadSuccess(String data, int what) {
        if (0 == what) {
            ProductDetailsBean bean = JSON.parseObject(data, ProductDetailsBean.class);
            if (null != bean) {
                Bundle bundle = new Bundle();
                bundle.putParcelable("orderbean", mOrderDetailsBean);
                bundle.putString("contractUkid", contractUkid);
                try {
                    setData(bean.getTotal(), 4, "com.wwwarehouse.contract.adjust_contract.ConstractProductDetailsFragment", bundle, bean.getList());
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }
    }

    @Override
    public void onLoadError(String errorInfo) {
        ToastUtils.showToast(errorInfo);
    }
}
